from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.permissions import IsAuthenticated
from rest_framework.generics import ListAPIView, CreateAPIView, UpdateAPIView, DestroyAPIView
from firstApp.models import Snippet
from firstApp.serializers import Snippet2Serializer
from rest_framework.filters import SearchFilter, OrderingFilter
from rest_framework.pagination import PageNumberPagination


class HelloView(APIView):
    permission_classes = (IsAuthenticated,)

    def get(self, request):
        print('authenticators:', request.authenticators)
        print('successful_authenticator:', request.successful_authenticator)
        print('authenticate:', request.successful_authenticator.authenticate(request))
        print('authenticate_header:', request.successful_authenticator.authenticate_header(request))
        print('get_header:', request.successful_authenticator.get_header(request))
        print('get_raw_token:',
              request.successful_authenticator.get_raw_token(request.successful_authenticator.get_header(request)))
        print('get_validated_token:', request.successful_authenticator.get_validated_token(
            request.successful_authenticator.get_raw_token(request.successful_authenticator.get_header(request))))
        print('get_user:', request.successful_authenticator.get_user(
            request.successful_authenticator.get_validated_token(
                request.successful_authenticator.get_raw_token(request.successful_authenticator.get_header(request)))))
        print('www_authenticate_realm:', request.successful_authenticator.www_authenticate_realm)
        return Response('OK')


class SnippetListAPIView(ListAPIView):
    """
        List 返回全部
    """
    queryset = Snippet.objects.all()
    serializer_class = Snippet2Serializer


class SnippetSearchListAPIView(ListAPIView):
    """搜索接口"""
    queryset = Snippet.objects.all()
    serializer_class = Snippet2Serializer
    # 第二步：局部配置 过滤类 们（全局配置用DEFAULT_FILTER_BACKENDS）
    filter_backends = [SearchFilter]
    # 第三步：SearchFilter过滤类依赖的过滤条件 => 接口：/cars/?search=...
    search_fields = ['code', 'language']


class SnippetOrderListAPIView(ListAPIView):
    """排序接口"""
    queryset = Snippet.objects.all()
    serializer_class = Snippet2Serializer
    filter_backends = [OrderingFilter]
    ordering_fields = ['id', 'created']


class SnippetPageNumberPagination(PageNumberPagination):
    # ?page=页码
    page_query_param = 'page'
    # ?page=页面 下默认一页显示的条数
    page_size = 3
    # ?page=页面&page_size=条数 用户自定义一页显示的条数
    page_size_query_param = 'page_size'
    # 用户自定义一页显示的条数最大限制：数值超过5也只显示5条
    max_page_size = 5


class SnippetPaginationListAPIView(ListAPIView):
    """分页接口"""
    queryset = Snippet.objects.all()
    serializer_class = Snippet2Serializer
    pagination_class = SnippetPageNumberPagination


class SnippetMyFilterListAPIView(ListAPIView):
    """自定义过滤器"""
    queryset = Snippet.objects.all()
    serializer_class = Snippet2Serializer

    # 除全局配置外 也可局部配置
    # filter_backends = [LimitFilter]


class SnippetCreateAPIView(CreateAPIView):
    queryset = Snippet.objects.all()
    serializer_class = Snippet2Serializer
    permission_classes = (IsAuthenticated,)


class SnippetUpdateAPIView(UpdateAPIView):
    queryset = Snippet.objects.all()
    serializer_class = Snippet2Serializer
    permission_classes = (IsAuthenticated,)


class SnippetDestroyAPIView(DestroyAPIView):
    queryset = Snippet.objects.all()
    serializer_class = Snippet2Serializer
    permission_classes = (IsAuthenticated,)
